package com.hospital.new_yygh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.new_yygh.service.DoctorService;
import com.hospital.new_yygh.entity.user.Doctor;
import com.hospital.new_yygh.entity.result.Result;
import com.hospital.new_yygh.entity.vo.DoctorVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "医生")
@RestController
@RequestMapping("/doctor")
@CrossOrigin
public class DoctorController {

    //注入service
    @Autowired
    private DoctorService doctorService;

    //医生登录接口
    @ApiOperation(value = "医生登录")
    @PostMapping("/doctorLogin/{username}/{password}")
    public Result doctorLogin(@PathVariable String username,
                              @PathVariable String password){
        QueryWrapper<Doctor> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        Doctor doctor = doctorService.getOne(wrapper);
        //查询管理员是否存在
        if (doctor != null && doctor.getPassword().equals(password)){
            //管理员存在且密码正确
            return Result.ok(doctor);
        }else{
            return Result.fail();
        }
    }

    //查询所有医生信息
    @ApiOperation(value = "获取所有医生信息")
    @GetMapping("findAllDoctor")
    public Result findAllDoctor(){
        //调用service中的方法
        List<Doctor> list = doctorService.list();
        return Result.ok(list);
    }

    //逻辑删除医生
    @ApiOperation(value = "逻辑删除医生")
    @DeleteMapping("removeDoctor/{id}")
    public Result removeDoctor(@PathVariable Long id){
        boolean flag = doctorService.removeById(id);
        if (flag){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //条件查询医生信息
    @ApiOperation(value = "条件查询医生信息")
    @PostMapping("findPageDoctor/{current}/{limit}")
    public Result findPageDoctor(@PathVariable long current,
                                 @PathVariable long limit,
                                 @RequestBody(required = false) DoctorVo doctorVo){
        //创建page对象，传递当前页，每页记录数
        Page<Doctor> page = new Page<>(current,limit);
        //构建条件
        QueryWrapper<Doctor> wrapper  = new QueryWrapper<>();
        String doctorName = doctorVo.getDoctorName();//医生姓名
        String ksId = doctorVo.getKsId();//科室编号
        if (!StringUtils.isEmpty(doctorName)){
            wrapper.like("doctorName",doctorName);
        }
        if (!StringUtils.isEmpty(ksId)){
            wrapper.eq("ksId",ksId);
        }
        //调用方法实现分页查询
        Page<Doctor> pageDoctor = doctorService.page(page, wrapper);
        //返回结果
        return Result.ok(pageDoctor);
    }

    //添加医生
    @ApiOperation(value = "添加医生")
    @PostMapping("saveDoctor")
    public Result saveDoctor(@RequestBody Doctor doctor){
        boolean save = doctorService.save(doctor);
        if (save){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //根据id获取医生信息
    @ApiOperation(value = "根据id获取医生信息")
    @GetMapping("getDoctor/{id}")
    public Result getDoctor(@PathVariable Long id){
        Doctor doctor = doctorService.getById(id);
        return Result.ok(doctor);
    }

    //修改医生信息
    @ApiOperation(value = "修改医生信息")
    @PostMapping("updateDoctor")
    public Result updateDoctor(@RequestBody Doctor doctor){
        boolean b = doctorService.updateById(doctor);
        if (b){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //批量删除医生
    @ApiOperation(value = "批量删除医生")
    @DeleteMapping("batchRemoveDoctor")
    public Result batchRemoveDoctor(@RequestBody List<Long> idList){
        boolean b = doctorService.removeByIds(idList);
        if (b){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }
}
